package com.fizzkai.p2p.website.controller;

import com.fizzkai.p2p.core.domain.LoginInfo;
import com.fizzkai.p2p.core.domain.OrderTime;
import com.fizzkai.p2p.core.domain.UserInfo;
import com.fizzkai.p2p.core.service.ILoginInfoService;
import com.fizzkai.p2p.core.service.IOrderTimeService;
import com.fizzkai.p2p.core.service.IUserInfoService;
import com.fizzkai.p2p.core.service.IVideoAuthService;
import com.fizzkai.p2p.core.util.AjaxResult;
import com.fizzkai.p2p.core.util.UserContext;
import com.fizzkai.p2p.website.util.RequireLogin;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class VideoAuthController {

    @Autowired
    private ILoginInfoService loginInfoService;
    @Autowired
    private IOrderTimeService orderTimeService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IVideoAuthService videoAuthService;


    // 预约视频 页面
    @RequestMapping("/videoAuditOrder")
    @RequireLogin
    public String videoAuditOrder(Model model){
        // 判断 有没有申请 视频验证
        Long userId = UserContext.getCurrentLoginInfo().getId();
        UserInfo userInfo = userInfoService.get(userId);
        // 判断 实名认证状态
        if (userInfo.isHasVideoAuth()) {
            // 视频验证了
            model.addAttribute("auditing", false);
            model.addAttribute("videoAuth", videoAuthService.get(userInfo.getVideoAuthId()));
            return "videoOrder_result";
        }
        // 判断 有无申请
        if (userInfo.getVideoAuthId() == null) {
            // 没有 申请 视频验证
            return unApplyVideoAuth(model);
        }
        // 申请了
        model.addAttribute("auditing", true);
        model.addAttribute("videoAuth", videoAuthService.get(userInfo.getVideoAuthId()));
        return "videoOrder_result";
    }
    // 没有 预约 视频验证
    private String unApplyVideoAuth(Model model) {
        List<LoginInfo> auditors =  loginInfoService.selectIsAuditorList();
        // 获取 全部 日期
        List<Date> orderDates = new ArrayList<>();
        Date now = new Date();
        orderDates.add(now);
        for (int i = 0; i < 4; i++) {
            orderDates.add(DateUtils.addDays(now, i + 1));
        }
        // 获取 全部 时间
        List<OrderTime> orderTimes = orderTimeService.list();

        model.addAttribute("auditors", auditors);
        model.addAttribute("orderDates", orderDates);
        model.addAttribute("orderTimes", orderTimes);
        return "videoOrder";
    }


    /**
     * 预约视频验证
     * @return
     */
    @RequestMapping("/videoAuth_apply")
    @ResponseBody
    public Object videoAuth_apply(Long timeid, String orderDate, Long auditorId){
        videoAuthService.apply( timeid,  orderDate,  auditorId);
        return new AjaxResult("预约成功");
    }



}
